<?php
// $[a-zA-Z][a-zA-Z0-9_]*
// 弱型別程式語言PHP, JavaScript => 強型別 Java
$var1 = 123;
echo gettype($var1) . '<br />'; //integer
$var1 = 12.3;
echo gettype($var1) . '<br />'; //double
$var1 = 'Brad';
echo gettype($var1) . '<br />'; //string
$var1 = false;
echo gettype($var1) . '<br />'; //boolean
$var1 = new DateTime();
echo gettype($var1) . '<br />'; //object
?>
強型別:宣告後不能改變 ex:Int
宣告變數不要用$$、$_ : 有特殊用處,並且大小寫嚴格區分
. :連接字串
<?php
$var1 = 'Brad';
$var2 = 'Chao';
$var3 = "$var1$var2" . '<br />';
$var4 = 59;
echo $var3; //BradChao
echo "{$var4}kg"; //59kg
?>
"{$變數}字串" >>> ""、''有差異
<script>
function add() {}
</script>
<form action="brad08_收取資料.php" method="get">
<!-- get是預設,改成method="post"會送不出去 -->
<!-- action="brad08_收取資料.php"沒給出去會留在原地(載入,有帶參數)-->
<input type="number" id="x" name="x" />
<span>+</span>
<input type="number" id="y" name="y" l>
<input type="button" onclick="add()" value="=(JS)" />
<input type="submit" value="=(PHP)" />
</form>
PHP會送出而刷新頁面(或送去別的PHP處理),但(X+X=?)用JS寫就好了
method="get":抓住,預設,如沒給出去會留在原地自己處理(有帶參數)
action="brad08.php":送去這PHP處理
// brad08.php內
<?php
$x = $_GET['x'];
$y = $_GET['y'];
$result = $x + $y;
echo "{$x} + {$y} = {$result}";
?>
指令、函數沒有嚴格區分
(1)變數
<?php
$a = 123;
$A = 456;
echo $A;
?>
<?php
$_GET = 124;
echo $_GET;
?>
(2)指令
<?php
$a = 123;
$A = 456;
eCho $A;
?>
(3)函數
<?php
$a = 123;
$A = 456;
eCho GetType($a);
?>
<?php
$x = $_GET['x'];
$y = $_GET['y'];
$result = $x + $y; //JS要轉型數值才能+ 否則會"字串"+"字串"
// echo "{$x} + {$y} = {$result}"; //不顯示在網頁上
?>
<script>
function add() {}
</script>
<form method="get">
<input type="number" id="x" name="x" value="<?php echo $x ?>" />
<span>+</span>
<input type="number" id="y" name="y" value="<?php echo $y ?>">
<input type="button" onclick="add()" value="=(JS)" />
<input type="submit" value="=(PHP)" />
<!-- 答案放這裡 -->
<span><?php echo $result ?></span>
</form>
自己get,放答案~
PHP>>送去阿帕契(程式,嵌入(似link:CSS)),檢視原始碼不會有PHP
HTML>>送去瀏覽器(文件)
記得對外營業的時候關掉,才不會被人家發現哪裡錯誤XD
(1)檔案brad02.php
(2)找
Loaded Configuration File
C:\MAMP\conf\php7.4.1\php.ini
(3)編輯php.ini >> display_errors = On
(4)重新開伺服器
<?php
$aaa = 123;
if (isset($aaa)) {
echo $aaa;
} else {
echo 'xxxxx';
}
?>
不使用isset也可以跑,但實際上是錯誤(職場上有人會這樣寫)
<?php
$aaa = 123;
if ($aaa) {
echo $aaa;
} else {
echo 'xxxxx';
}
?>
<?php
$result = $op = ''; //預設值
if (isset($_GET['x']) && isset($_GET['y'])) {
//isset帶一個name:屬性,其他會跟著帶,
//這是http的通訊協定,所以可以只寫isset($_GET['x'])
$op = $_GET['op'];
$x = $_GET['x'];
$y = $_GET['y']; // JS要轉型數值才能+ 否則會"字串"+"字串"
$result = $x + $y;
}
?>
if ($op == 1) {
$result = $x + $y;
} else if ($op == 2) {
$result = $x - $y;
} else if ($op == 3) {
$result = $x * $y;
} else if ($op == 4) {
$result = (int)($x / $y) . ' ...... ' . ($x % $y); //強制轉型整數
} else {
$result = "";
}
$score = rand(0,100);
echo $score . '<hr />';
if ($score >= 90){
echo 'A';
}else if ($score >= 80){
echo 'B';
}else if ($score >= 70){
echo 'C';
}else if ($score >= 60){
echo 'D';
}else {
echo 'E';
}
https://www.php.net/manual/en/function.rand
$var1 = 10;
$var2 = 20;
switch ($var1) {
case 1:
echo 'A';
break;
case 2:
echo 'A1';
break;
case $var2 - 10: //要=var1
echo 'B';
break;
default:
echo 'C';
}
$i = 0;
for (test1(); $i < 10; drawLine()) {
// test1()拿來做一件事情
// $i = 0;在外面已經附值
echo "{$i}<br />";
$i++;
// drawLine()拿來做一件事情
//$i++;拿出來~
}
function test1()
{
echo 'Brad<br />';
}
function drawLine()
{
echo '<hr />';
}
2x1=2
2x1=2
2x1=2
2x1=2
2x1=2
for($i=1從1出發 ;$i<=9乘以9次 ;$i++){
echo "2 x 1 = 2<br>";
}
for ($i = 1; $i <= 9; $i++) {
$r = 2 * $i; //$r裝答案
echo " 2 x {$i} = {$r} <br> ";
}
echo '<td>';
for ($i = 1; $i <= 9; $i++) {
$r = 2 * $i; //$r裝答案
echo " 2 x {$i} = {$r} <br> \n";
}
echo '<td>';
for ($j = 2; $j <= 5; $j++) {
echo '<td>';
for ($i = 1; $i <= 9; $i++) {
$r = 2 * $i; //$r裝答案
echo " 2 x {$i} = {$r} <br>\n";
}
echo '<td>';
}
for ($j = 2; $j <= 5; $j++) {
echo '<td>';
for ($i = 1; $i <= 9; $i++) {
$r = 2 * $i; //$r裝答案
echo " {$j} x {$i} = {$r} <br>\n";
}
echo '<td>';
}
echo '<tr>';
for ($j = 2; $j <= 5; $j++) {
echo '<td>';
for ($i = 1; $i <= 9; $i++) {
$r = 2 * $i; //$r裝答案
echo " {$j} x {$i} = {$r} <br>\n";
}
echo '<td>';
}
echo '<tr>';
for ($k = 0; $k < 2; $k++) {
echo '<tr>';
for ($j = 2; $j <= 5; $j++) {
echo '<td>';
for ($i = 1; $i <= 9; $i++) {
$r = 2 * $i; //$r裝答案
echo " {$j} x {$i} = {$r} <br>\n";
}
echo '<td>';
}
echo '<tr>';
}
調整$j ==> $newj = $j + $k * 4;
有先*/後+-
第一圈要保留,從第二圈{$k}開始改
04 = 0 :2+0=2、3+0=3、4+0=4、5+0=5(上排,第一圈)
14 = 4 :2+4=6、3+4=7、4+4=8....(下排,第二圈)
for ($k = 0; $k <= 1; $k++) {
echo '<tr>';
for ($j = 2; $j <= 5; $j++) {
$newj = $j + $k * 4;
echo '<td>';
for ($i = 1; $i <= 9; $i++) {
$r = 2 * $i; //$r裝答案
echo " {$newj} x {$i} = {$r} <br>\n";
}
echo '<td>';
}
echo '<tr>';
}
define("ROW", 4);
define("COL", 4);
define("START", 2);
for ($k = 0; $k < ROW; $k++) {
echo '<tr>';
for ($j = START; $j < START + COL; $j++) {
$newj = $j + $k * COL;
if (($j + $k) % 2 == 0) {
echo '<td bgcolor="yellow">';
} else {
echo '<td bgcolor="pink">';
}
for ($i = 1; $i <= 9; $i++) {
$r = $newj * $i;
echo "{$newj} x {$i} = {$r}<br />\n";
}
echo '</td>';
}
echo '</tr>';
}
改成常數,之後就會很好控制~彈性高!常數不用$